//
//  SeachViewController.m
//  OneDoller
//
//  Created by jt on 16/8/16.
//  Copyright © 2016年 jyj. All rights reserved.
//

#import "SeachViewController.h"
#import "Masonry.h"

@interface SeachViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (strong,nonatomic) UISearchBar *SearchBar;

@property (strong,nonatomic) UIView *SeachView;
@property (strong,nonatomic) UITextField *SeachField;
@property (strong,nonatomic) UIButton *BackButt;

@property (strong,nonatomic) UIView *HitView;
@property (strong,nonatomic) UILabel *HitLabel;

@property (strong,nonatomic) UITableView *HistoryView;
@property (strong,nonatomic) NSMutableArray *HistoryArr;
@property (strong,nonatomic) UILabel *HistoryLabel;

@end

@implementation SeachViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _HistoryArr = @[@1,@2,@"ss",@"sssq",@"qqqq",@"daadsasd"];
//    隐藏
//设置第一响应者
    [_SeachField becomeFirstResponder];
    
    self.view.backgroundColor = [UIColor blueColor];
    _SeachView = [[UIView alloc] init];
    _SeachView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 0.1);
    _SeachField = [[UITextField alloc] init];
    _SeachField.backgroundColor = [UIColor grayColor];
  
    
//    SeachView和里面控件进行布局
    
     [self.view addSubview:_SeachView];
    [_SeachView addSubview:_SeachField];
    [_SeachField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_SeachView.mas_left).offset(15);
        make.top.equalTo(_SeachView.mas_top).offset(20);
        make.width.equalTo(_SeachView.mas_width).multipliedBy(0.7);
        make.height.equalTo(_SeachView.mas_height).multipliedBy(0.5);
        make.height.mas_equalTo(30);
    }];
//
    _BackButt = [UIButton buttonWithType:UIButtonTypeSystem];
    [_BackButt setTitle:@"取消" forState:UIControlStateNormal];
    [_BackButt addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    
    [_SeachView addSubview:_BackButt];
    
    [_BackButt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_SeachView.mas_right).offset(-10);
        make.top.equalTo(_SeachView.mas_top).offset(20);
        make.width.equalTo(_SeachView.mas_width).multipliedBy(0.1);
        make.height.mas_equalTo(30);
    }];
    
    _SeachView.backgroundColor = [UIColor orangeColor];
   
    
//  对HitView和里面控件进行布局
    
    _HitView = [[UIView alloc] init];
    _HitView.frame = CGRectMake(0, self.view.bounds.size.height * 0.1, self.view.bounds.size.width, self.view.bounds.size.height * 0.9);
    _HitView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_HitView];
    UIButton *butt1 = [UIButton buttonWithType:UIButtonTypeSystem];
    
    butt1.backgroundColor = [UIColor whiteColor];
    [butt1 setTitle:@"皮卡丘" forState:UIControlStateNormal];
    [_HitView addSubview:butt1];
    
    [butt1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_HitView.mas_left).offset(20);
//        make.right.equalTo(_HitView.mas_right).offset(-(self.view.bounds.size.width -100 ));
        make.top.equalTo(_HitView.mas_top).offset(40);
//        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
        
    }];
     UIButton *butt2 = [UIButton buttonWithType:UIButtonTypeSystem];
    butt2.backgroundColor = [UIColor whiteColor];
    [butt2 setTitle:@"可达鸭" forState:UIControlStateNormal];
    [_HitView addSubview:butt2];
    
    [butt2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(butt1.mas_right).offset(10);
//        make.right.equalTo(butt3.mas_left).offset(-10);
        make.top.equalTo(_HitView.mas_top).offset(40);
//        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
        
    }];
     UIButton *butt3 = [UIButton buttonWithType:UIButtonTypeSystem];
    butt3.backgroundColor = [UIColor whiteColor];
    [butt3 setTitle:@"喷火龙" forState:UIControlStateNormal];
    [_HitView addSubview:butt3];
    
    [butt3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(butt2.mas_right).offset(10);
        
        make.top.equalTo(_HitView.mas_top).offset(40);
//        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
        
    }];
     UIButton *butt4 = [UIButton buttonWithType:UIButtonTypeSystem];
    butt4.backgroundColor = [UIColor whiteColor];
    [butt4 setTitle:@"急冻鸟" forState:UIControlStateNormal];
    [_HitView addSubview:butt4];
    
    [butt4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(butt3.mas_right).offset(10);
//                make.right.equalTo(_HitView.mas_right).offset(-(self.view.bounds.size.width -100 ));
        make.top.equalTo(_HitView.mas_top).offset(40);
//        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
        
    }];
     UIButton *butt5 = [UIButton buttonWithType:UIButtonTypeSystem];
    butt5.backgroundColor = [UIColor whiteColor];
    [butt5 setTitle:@"杰尼龟" forState:UIControlStateNormal];
    [_HitView addSubview:butt5];
    
    [butt5 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_HitView.mas_left).offset(20);
        //        make.right.equalTo(_HitView.mas_right).offset(-(self.view.bounds.size.width -100 ));
        make.top.equalTo(_HitView.mas_top).offset(90);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
        
    }];
    

    if (_HistoryArr != nil) {
        //        对HistoryView进行布局
        _HistoryLabel.text = @"历史搜索";
        _HistoryView = [[UITableView alloc] initWithFrame:CGRectMake(0, 270, self.view.bounds.size.width, self.view.bounds.size.height - 270) style:UITableViewStylePlain];
        //        [_HistoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //            <#code#>
        //        }];
        _HistoryView.delegate = self;
        _HistoryView.dataSource = self;
        
        [self.view addSubview:_HistoryView];
    }

    
}




#pragma mark - backTo

-(void) backTo{

    [self.navigationController popViewControllerAnimated:YES];

}

#pragma mark - UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _HistoryArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",_HistoryArr[indexPath.row]];
    
    return cell;
}
#pragma mark - UITableViewDelegate

//cell允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//删除历史记录
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.HistoryArr removeObjectAtIndex:indexPath.row - 1];
        
        [self.HistoryView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
}

//修改编辑按钮文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

// 隐藏nav
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES]; }
//退出该控制器时还原：

- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController setNavigationBarHidden:NO]; }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
